@extends('layouts.application')

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">文章管理</strong> /
                    <small>Article list</small>
                </div>
            </div>

            <hr>
            @include('layouts.shared._flash')

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('crm.article.create')}}" class="am-btn am-btn-default"><span
                                        class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="delete_all am-btn am-btn-default"><span
                                        class="am-icon-trash-o"></span> 删除
                            </button>
                        </div>
                    </div>
                </div>

                <form action="" method="get">
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-form-group">
                            <select name="category_id" data-am-selected="{btnWidth: '100%',  btnStyle: 'secondary', btnSize: 'sm', maxHeight: 260, searchBox: 1}"">

                            <optgroup label="请选择">
                                <option value="-1">所有类别</option>
                            </optgroup>

                            @foreach($categories as $category)
                                <optgroup label="{{$category->name}}">

                                    @foreach($category->children as $child)
                                        <option value="{{$child->id}}">
                                            &nbsp;&nbsp;&nbsp;&nbsp;|--{{$child->name}}
                                        </option>
                                    @endforeach

                                </optgroup>
                            @endforeach
                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-3">
                        <div class="am-input-group am-input-group-sm">
                            <input type="text" name="title" value="{{Request::input('title')}}" class="am-form-field">
                            <span class="am-input-group-btn">
                                <button class="am-btn am-btn-default" type="submit">搜索</button>
                            </span>
                        </div>
                    </div>
                </form>
            </div>


            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>
                                    <input class="check_all" type="checkbox"/>
                                </th>
                                <th>编号</th>
                                <th>缩略图</th>
                                <th>标题</th>
                                <th>来源</th>
                                <th>所属栏目</th>
                                <th>修改日期</th>
                                <th>是否置顶</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($articles as $article)
                                <tr data-id="{{$article->id}}">
                                    <td>
                                        <input type="checkbox" class="checked_id" name="check_id[]"
                                               value="{{$article->id}}"/>
                                    </td>
                                    <td>{{$article->id}}</td>
                                    <td>
                                        <a href="#"><img src="{{$article->thumb}}" alt=""
                                                         style="width: 50px;height: 50px;"></a>
                                    </td>
                                    <td>{{str_limit($article->title,10,'...')}}</td>
                                    <td class="am-hide-sm-only">{{$article->from}}</td>
                                    <td class="am-hide-sm-only">{{$article->category->name or ''}}</td>
                                    <td class="am-hide-sm-only">{{$article->updated_at}}</td>
                                    <td>
                                        {!! is_something($article,'is_top') !!}
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('crm.article.edit',$article->id)}}"
                                                   class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                <button class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>

                        {{--分页--}}
                        @include('admin.crm.shared._page',['articles'=>$articles])


                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection

@section('js')
    <script>
        $(function () {
            //删除文章
            $('.delete').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);
                $.ajax({
                    type: 'DELETE',
                    url: "/admin/crm/article/" + id,
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                })
                return false;
            })

            //上面选中，下面也选中
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            });
            //下面全选，上面也选中
            $('.checked_id').click(function () {
                var all_length = $('.checked_id').length;
                var check_length = $('.checked_id:checked').length;
                if (all_length == check_length) {
                    $('.check_all').prop('checked', true);
                } else {
                    $('.check_all').prop('checked', false);
                }
            });

            //多选删除
            $('.delete_all').click(function () {
                var check_length = $('.checked_id:checked').length;
                if (check_length == 0) {
                    alert('请你至少选中一条');
                    return false;
                }

                $.ajax({
                    type: 'DELETE',
                    url: '{{route('crm.article.delete_all')}}',
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                })
                return false;
            });

            //改变属性---是否置顶
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).data('attr');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '{{route('crm.article.change_attr')}}',
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                })
                return false;
            })

        })
    </script>
@endsection